#include "dicomimagelabel.h"
#include "imageinstance.h"
#include "../MainStation/mainwindow.h"

#include <QFrame>
#include <QMouseEvent>
#include <QResizeEvent>
#include <QDrag>
#include <QMimeData>
#include <QApplication>
#include <QLabel>
#include <QAction>

DicomImageLabel::DicomImageLabel(ImageInstance *image, QWidget *parent) :
    nameTag(new QLabel(this)),
    frameTag(new QLabel(this)),
    imageInst(image),
    QLabel(parent)
{
    setStyleSheet(QString::fromLatin1("background-color: rgb(0, 0, 0);"));
    setFrameShape(QFrame::Box);
    setFrameShadow(QFrame::Plain);
    setLineWidth(2);
    setAlignment(Qt::AlignCenter);
    setContextMenuPolicy(Qt::ActionsContextMenu);

    QPalette p = nameTag->palette();
    p.setColor(QPalette::WindowText, Qt::white);
    nameTag->setPalette(p);
    frameTag->setPalette(p);
    QFont f = nameTag->font();
    f.setPointSize(9);
    nameTag->setFont(f);
    frameTag->setFont(f);

    init();
}

DicomImageLabel::~DicomImageLabel()
{
    delete imageInst;
    delete removeAction;
}

void DicomImageLabel::init()
{
    if (imageInst) {
        QPixmap pixmap;
        imageInst->setImageType(false);
        imageInst->getPixmap(pixmap);
        setPixmap(pixmap.scaled(IMAGE_LABEL_SIZE-2, IMAGE_LABEL_SIZE-2, Qt::KeepAspectRatio, Qt::SmoothTransformation));
        nameTag->setText(imageInst->patientName());
        ulong count = imageInst->getFrameCount();
        if (imageInst->hasMultiFrames()) {
            frameTag->setText(QString::number(count));
            frameTag->setVisible(true);
        } else frameTag->setVisible(false);
    }

    removeAction = new QAction(tr("Remove"), 0);
    connect(removeAction, SIGNAL(triggered()), this, SLOT(onRemoveImage()));
    removeAction->setEnabled(mainWindow->getCurrentGroup().permissions&GP_RemoveStudy);
    addAction(removeAction);
}

void DicomImageLabel::onRemoveImage()
{
    emit removeImage(imageInst);
}

void DicomImageLabel::setHighlight(bool yes)
{
    QPalette p = palette();
    if (yes) p.setColor(QPalette::WindowText, Qt::green);
    else p.setColor(QPalette::WindowText, Qt::black);
    setPalette(p);
}

void DicomImageLabel::mousePressEvent(QMouseEvent *e)
{
    emit imageClicked(this);
    dragOrg = e->pos();
    QLabel::mousePressEvent(e);
}

void DicomImageLabel::mouseMoveEvent(QMouseEvent *e)
{
    if ((e->buttons() & Qt::LeftButton) &&
            ((e->pos()-dragOrg).manhattanLength() > QApplication::startDragDistance())) {
        QDrag *drag = new QDrag(this);
        QMimeData *data = new QMimeData;
        data->setImageData((qulonglong)imageInst);
        drag->setMimeData(data);
        drag->exec(Qt::CopyAction);
    }
    QLabel::mouseMoveEvent(e);
}

void DicomImageLabel::resizeEvent(QResizeEvent *e)
{
    QLabel::resizeEvent(e);
    QSize size = e->size();
    QSize tagSize = frameTag->size();
    frameTag->setGeometry(size.width()-tagSize.width()-2,
                          size.height()-tagSize.height()-2,
                          tagSize.width(), tagSize.height());
}

void DicomImageLabel::mouseDoubleClickEvent(QMouseEvent *e)
{
    emit imageDoubleClicked(this);
    QLabel::mouseDoubleClickEvent(e);
}
